﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Proje
{
    public partial class SearchRoute : Form
    {
        private List<Route> routes;
        public SearchRoute()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            RouteFileConnector connector = new RouteFileConnector();
            routes = connector.searchRoute((string)startPoint.SelectedItem, 
                (string)endPoint.SelectedItem, 
                routeDate.Text);
            routeTable.DataSource = routes;
        }

        private void seat_click(object sender, EventArgs e)
        {
            string g = (string )gender.SelectedItem;
            if (g.Equals("Male"))
            {
                Button b = (Button)sender;
                int seatNo = Convert.ToInt32(b.Text);
                if (seatNo % 2 == 0)
                {
                    if (buttonList[seatNo - 2].BackColor == Color.Blue || buttonList[seatNo - 2].BackColor == Color.Green)
                    {
                        if (MessageBox.Show("Koltuk Satılsın mı?", "Onayla", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            Seat seat = new Seat();
                            seat.seatNo = seatNo;
                            seat.start = (string)startPoint.SelectedItem;
                            seat.end = (string)startPoint.SelectedItem;
                            seat.gender = g;
                            b.BackColor = Color.Blue;
                            new SeatFileConnector().insert(seat, routes[selectedRouteId].number);
                        }
                    }
                }
                else
                {
                    if (buttonList[seatNo].BackColor == Color.Blue || buttonList[seatNo].BackColor == Color.Green)
                    {
                        if (MessageBox.Show("Koltuk Satılsın mı?", "Onayla", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            Seat seat = new Seat();
                            seat.seatNo = seatNo;
                            seat.start = (string)startPoint.SelectedItem;
                            seat.end = (string)startPoint.SelectedItem;
                            seat.gender = g;
                            b.BackColor = Color.Blue;
                            new SeatFileConnector().insert(seat, routes[selectedRouteId].number);
                        }
                    }
                }
            }
            else
            {
                Button b = (Button)sender;
                int seatNo = Convert.ToInt32(b.Text);
                if (seatNo % 2 == 0)
                {
                    if (buttonList[seatNo - 2].BackColor == Color.Red || buttonList[seatNo - 2].BackColor == Color.Green)
                    {
                        if (MessageBox.Show("Koltuk Satılsın mı?", "Onayla", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            Seat seat = new Seat();
                            seat.seatNo = seatNo;
                            seat.start = (string)startPoint.SelectedItem;
                            seat.end = (string)startPoint.SelectedItem;
                            seat.gender = g;
                            b.BackColor = Color.Red;
                            new SeatFileConnector().insert(seat, routes[selectedRouteId].number);
                        }
                    }
                }
                else
                {
                    if (buttonList[seatNo - 2].BackColor == Color.Red || buttonList[seatNo - 2].BackColor == Color.Green)
                    {
                        if (MessageBox.Show("Koltuk Satılsın mı?", "Onayla", MessageBoxButtons.YesNo) == DialogResult.Yes)
                        {
                            Seat seat = new Seat();
                            seat.seatNo = seatNo;
                            seat.start = (string)startPoint.SelectedItem;
                            seat.end = (string)startPoint.SelectedItem;
                            seat.gender = g;
                            b.BackColor = Color.Red;
                            new SeatFileConnector().insert(seat, routes[selectedRouteId].number);
                        }

                    }
                }
            }
        }
        private List<Button> buttonList = new List<Button>();
        private static int selectedRouteId;
        private void show_Click(object sender, EventArgs e)
        {
            flowLayoutPanel1.Controls.Clear();
            selectedRouteId = routeTable.SelectedRows[0].Index;
            Route r = routes[selectedRouteId];
            string routeString = "";
            for (int i = 0; i < r.stopPoints.Count; i++)
            {
                if (r.stopPoints[i].name.Equals((string)startPoint.SelectedItem))
                {
                    routeString += (string)startPoint.SelectedItem;
                    for (int j = i+1; j < r.stopPoints.Count ; j++)
                    {
                        if (!r.stopPoints[j].name.Equals((string)endPoint.SelectedItem))
                        {
                            routeString += r.stopPoints[j].name;
                        }
                    }
                    routeString += (string)endPoint.SelectedItem;
                    break;
                }
            }
            List<Seat> list = new SeatFileConnector().getSeats(routes[selectedRouteId].number);
            for (int i = 0; i < 22; i++)
            {   
                
                System.Windows.Forms.Button button2=new System.Windows.Forms.Button();
                button2.Name = "" + (i + 1);
                button2.Text = "" + (i + 1);
                button2.Size = new System.Drawing.Size(60,30);
                button2.UseVisualStyleBackColor = true;
                button2.Click += seat_click;
                button2.BackColor = Color.Green;
                buttonList.Add(button2);
                for (int j = 0; j < list.Count; j++)
                {
                    if (list[j].seatNo == i + 1)
                    {
                        if (routeString.Contains(list[j].start) || routeString.Contains(list[j].end)) 
                        {
                            if (list[j].gender.Equals("Male"))
                            {
                                button2.BackColor = Color.Blue;
                            }
                            else
                            {
                                button2.BackColor = Color.Red;
                            }
                            button2.Click -= seat_click;
                        }

                    }
                }
                this.flowLayoutPanel1.Controls.Add(button2);
                
            }
            System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
            button1.Name = "kapi1";
            button1.Text = "Kapı";
            button1.Size = new System.Drawing.Size(60, 30);
            button1.UseVisualStyleBackColor = true;
            button1.Click += seat_click;
            this.flowLayoutPanel1.Controls.Add(button1);
            button1.Enabled = false;
            System.Windows.Forms.Button button3 = new System.Windows.Forms.Button();
            button3.Name = "kapi1";
            button3.Text = "Kapı";
            button3.Size = new System.Drawing.Size(60, 30);
            button3.UseVisualStyleBackColor = true;
            button3.Click += seat_click;
            button3.Enabled = false;
            this.flowLayoutPanel1.Controls.Add(button3);
            for (int i = 22; i < 46; i++)
            {

                System.Windows.Forms.Button button2 = new System.Windows.Forms.Button();
                button2.Name = "" + i + 1;
                button2.Text = "" + (i + 1);
                button2.Size = new System.Drawing.Size(60, 30);
                button2.UseVisualStyleBackColor = true;
                button2.Click += seat_click;
                button2.BackColor = Color.Green;
                buttonList.Add(button2);
                for (int j = 0; j < list.Count; j++)
                {
                    if (list[j].seatNo == i + 1)
                    {
                        if (routeString.Contains(list[j].start) || routeString.Contains(list[j].end))
                        {
                            if (list[j].gender.Equals("Male"))
                            {
                                button2.BackColor = Color.Blue;
                            }
                            else
                            {
                                button2.BackColor = Color.Red;
                            }
                            button2.Click -= seat_click;
                        }

                    }
                }
                this.flowLayoutPanel1.Controls.Add(button2);

            }
            
        }
        
    }
}
